import 'package:blibli/root-page/home-page.dart';
import 'package:blibli/root-page/music-page.dart';
import 'package:blibli/root-page/profile-page.dart';
import 'package:blibli/root-page/video-page.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

final Map<String, String> _tabMap = {
  'home': '首页',
  'music': '音乐',
  'create_media': '',
  'tiny_video': '视频',
  'profile': '我的',
};
final List<Widget> pages = [
  MyHomePage(),
  MusicPage(),
  Container(),
  VideoPage(),
  ProfilePage()
];

BottomNavigationBarItem _createNavBarItem(String key, String value) {
  return BottomNavigationBarItem(
    icon: Image.asset(
      'assets/icons/${key}.png',
      width: 24,
      height: 24,
    ),
    activeIcon: Image.asset(
      'assets/icons/${key}_active.png',
      width: 24,
      height: 24,
    ),
    label: value,
    tooltip: '', // 去掉长按显示的小提示框
  );
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  List<BottomNavigationBarItem> bottomList = [];

  void _onTapClick(int index) {
    if (index == 2) return _onTapHandle();
    setState(() {
      _currentIndex = index;
    });
  }

  void _onTapHandle() {
    print('fa bu');
  }

  Widget _floatBtn() {
    return Container(
      width: 44,
      height: 44,
      margin: EdgeInsets.only(top: 56),
      child: FloatingActionButton(
        child: Image.asset('assets/icons/create_media.png'),
        onPressed: _onTapHandle,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _tabMap.forEach((key, value) {
      bottomList.add(_createNavBarItem(key, value));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: pages[_currentIndex],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomList,
        currentIndex: _currentIndex,
        onTap: _onTapClick,
        type: BottomNavigationBarType.fixed,
        selectedFontSize: 12,
      ),
      floatingActionButton: _floatBtn(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}
